﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chizx.Common
{
    /// <summary>
    /// Date日期封装工具类
    /// @author Chitry
    /// @email  chitry@126.com
    /// @website http://www.chitry.com
    /// </summary>
    public class DateHelper
    {
        /// <summary>
        /// 返回当前的格林尼治标准时间戳
        /// </summary>
        /// <returns></returns>
        public static DateTime getUtcNow()
        {
            DateTime utcNow = DateTime.Now.ToUniversalTime();
            return utcNow;
        }

      

         /// <summary>
         /// 根据时间返回几个月前,几天前,几小时前,几分钟前,几秒前
         /// </summary>
         /// <param name="dt"></param>
         /// <returns></returns>
         public static string DateStringFromNow(DateTime dt)
         {
             TimeSpan span = DateTime.Now - dt;
             if (span.TotalDays > 60)
             {
                 return dt.ToShortDateString();
             }
             else if (span.TotalDays > 30)
             {
                 return "1个月前";
             }
             else if (span.TotalDays > 14)
             {
                 return "2周前";
             }
             else if (span.TotalDays > 7)
             {
                 return "1周前";
             }
             else if (span.TotalDays > 1)
             {
                 return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
             }
             else if (span.TotalHours > 1)
             {
                 return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
             }
             else if (span.TotalMinutes > 1)
             {
                 return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
             }
             else if (span.TotalSeconds >= 1)
             {
                 return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
             }
             else
             {
                 return "1秒前";
             }
         }
    }
}
